<template>
    <div class="custom-loading-mask" v-show="loading">
      <div class="custom-loading-container">
        <el-icon class="is-loading" :size="iconSize">
          <Loading />
        </el-icon>
        <div class="custom-loading-text">{{ currentText }}</div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, onMounted, onBeforeUnmount,watch } from 'vue'
  import { Loading } from '@element-plus/icons-vue'
  
  const props = defineProps({
    loading: {
      type: Boolean,
      default: false
    },
    texts: {
      type: Array,
      default: () => [
        '正在加载数据...',
        '即将完成...',
        '请稍候片刻...',
        '努力加载中...',
        '马上就好了...'
      ]
    },
    iconSize: {
      type: Number,
      default: 32
    }
  })
  
  const currentText = ref(props.texts[0])
  let timer = null
  
  // 随机切换文字
  const rotateText = () => {
    const randomIndex = Math.floor(Math.random() * props.texts.length)
    currentText.value = props.texts[randomIndex]
  }
  
  // 启动定时器
  onMounted(() => {
    if (props.loading) {
      timer = setInterval(rotateText, 2000)
    }
  })
  
  // 监听loading状态变化
  watch(() => props.loading, (newVal) => {
    if (newVal) {
      timer = setInterval(rotateText, 2000)
    } else {
      clearInterval(timer)
      timer = null
    }
  })
  
  // 清除定时器
  onBeforeUnmount(() => {
    clearInterval(timer)
  })
  </script>
  
  <style scoped>
  .custom-loading-mask {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 9999;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: opacity 0.3s;
  }
  
  .custom-loading-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background-color: rgba(255, 255, 255, 0.9);
    padding: 30px 40px;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }
  
  .custom-loading-text {
    margin-top: 15px;
    color: #606266;
    font-size: 16px;
    text-align: center;
    min-width: 150px;
  }
  
  .el-icon.is-loading {
    animation: rotating 2s linear infinite;
  }
  
  @keyframes rotating {
    0% {
      transform: rotate(0deg);
    }
    100% {
      transform: rotate(360deg);
    }
  }
  </style>